package tt.dz.biz.dao.resource;

import java.util.List;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.vo.ResourceVO;
import tt.dz.vo.RoleResourceVO;
import tt.dz.vo.RoleVO;

@Repository("resourceDAO")
public class ResourceDAOImpl
  extends BaseDAO
{
  private static final String QUERY_RESOURCE_BY_ROLE = "select t1.resource_id, t1.url, t1.resource_desc,t1.created_by, t1.created_time, t1.updated_by, t1.updated_time, t1.sort, t1.parent_resource_id from ms_sec_resource t1 inner join ms_sec_role_res t2 on t1.is_valid=1 and t2.is_valid=1 and t1.resource_id = t2.resource_id and t2.role_id = :roleId";
  private static final String QUERY_UN_ALLOT_RESOURCE = "(select t1.resource_id, t1.url, t1.resource_desc,t1.created_by, t1.created_time, t1.updated_by, t1.updated_time, t1.sort, t1.parent_resource_id from ms_sec_resource t1 where t1.is_valid = 1 )except (select t1.resource_id, t1.url, t1.resource_desc,t1.created_by, t1.created_time, t1.updated_by, t1.updated_time, t1.sort, t1.parent_resource_id from ms_sec_resource t1 inner join ms_sec_role_res t2 on t1.is_valid=1 and t2.is_valid=1 and t1.resource_id = t2.resource_id and t2.role_id = :roleId)";
  private static final String INSERT_ROLE_RESOURCE = "insert into ms_sec_role_res(role_id,resource_id,created_by,created_time,updated_by,updated_time,is_valid) values (:roleId,:resourceId,:createdBy,:createdTime,:updatedBy,:updatedTime,:isValid)";
  private static final String DELETE_ROLE_RESOURCE = "update ms_sec_role_res set is_valid = 0 where role_id = :roleId and resource_id = :resourceId";
  private static final String UPDATE_ROLE_RESOURCE = "update ms_sec_role_res set is_valid = 1 where role_id = :roleId and resource_id = :resourceId";
  private static final String QUERY_ROLE_RESOURCE_COUNT = "select count(1) from ms_sec_role_res where role_id = :roleId and resource_id = :resourceId";
  private static final String QUERY_RESOURCE_COUNT = "select count(1) from ms_sec_resource where resource_id = :resourceId and is_valid = 1";
  
  public List<ResourceVO> queryResourceByRole(RoleVO roleVO)
  {
    return queryForList("select t1.resource_id, t1.url, t1.resource_desc,t1.created_by, t1.created_time, t1.updated_by, t1.updated_time, t1.sort, t1.parent_resource_id from ms_sec_resource t1 inner join ms_sec_role_res t2 on t1.is_valid=1 and t2.is_valid=1 and t1.resource_id = t2.resource_id and t2.role_id = :roleId", roleVO, ResourceVO.class);
  }
  
  public List<ResourceVO> queryUnAllotResourceByRole(RoleVO roleVO)
  {
    return queryForList("(select t1.resource_id, t1.url, t1.resource_desc,t1.created_by, t1.created_time, t1.updated_by, t1.updated_time, t1.sort, t1.parent_resource_id from ms_sec_resource t1 where t1.is_valid = 1 )except (select t1.resource_id, t1.url, t1.resource_desc,t1.created_by, t1.created_time, t1.updated_by, t1.updated_time, t1.sort, t1.parent_resource_id from ms_sec_resource t1 inner join ms_sec_role_res t2 on t1.is_valid=1 and t2.is_valid=1 and t1.resource_id = t2.resource_id and t2.role_id = :roleId)", roleVO, ResourceVO.class);
  }
  
  public int deleteRoleResource(RoleResourceVO roleResourceVO)
  {
    return save("update ms_sec_role_res set is_valid = 0 where role_id = :roleId and resource_id = :resourceId", roleResourceVO);
  }
  
  public int updateRoleResource(RoleResourceVO roleResourceVO)
  {
    return save("update ms_sec_role_res set is_valid = 1 where role_id = :roleId and resource_id = :resourceId", roleResourceVO);
  }
  
  public int insertRoleResource(RoleResourceVO roleResourceVO)
  {
    return save("insert into ms_sec_role_res(role_id,resource_id,created_by,created_time,updated_by,updated_time,is_valid) values (:roleId,:resourceId,:createdBy,:createdTime,:updatedBy,:updatedTime,:isValid)", roleResourceVO);
  }
  
  public int getRoleResourceCount(RoleResourceVO roleResourceVO)
  {
    return queryForInt("select count(1) from ms_sec_role_res where role_id = :roleId and resource_id = :resourceId", roleResourceVO);
  }
  
  public int getResourceCount(RoleResourceVO roleResourceVO)
  {
    return queryForInt("select count(1) from ms_sec_resource where resource_id = :resourceId and is_valid = 1", roleResourceVO);
  }
}
